package controllers

import (
	"newAi/new_web/models"
)

type RouteController struct {
	BaseController
}

func (self *RouteController) List() {
	self.Data["pageTitle"] = "路由列表"
	self.display()
}

func (self *RouteController) TaskTable() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//group 列表
	//查询条件
	count,result, err := models.GetRouteList(page,limit)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["dialplan_context"] = v.Dialplan_context
		row["dialplan_detail_tag"] = v.Dialplan_detail_tag
		row["dialplan_name"] = v.Dialplan_name
		row["dialplan_number"] = v.Dialplan_number
		row["data"] = v.Dialplan_detail_data
		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

func (self *RouteController) Add(){
	self.Data["pageTitle"] = "新增路由"
	self.display()
}

func (self *RouteController) Edit(){
	self.Data["pageTitle"] = "修改路由"
	id,_ := self.GetInt64("id")
	result, _:= models.GetRoute(id)
	self.Data["route"] = result
	self.display()
}

func (self *RouteController) AjaxSave() {
	if i, _ := self.GetInt64("id"); i != 0 {
		route := new(models.AiRoute)
		route.Dialplan_number = self.GetString("number")
		route.Dialplan_name = self.GetString("d_name")
		tag := self.GetString("tag")
		data := self.GetString("data")
		err := models.UpdateRoute(i,route.Dialplan_number,route.Dialplan_name,tag,data)
		if err != nil {
			self.ajaxMsg("正则不能重复", MSG_ERR)
			return
		}
		self.ajaxMsg("", MSG_OK)
		return
	}
	route := new(models.AiRoute)
	route.Dialplan_number = self.GetString("number")
	route.Dialplan_name = self.GetString("d_name")
	route.Dialplan_context = "destination_number"
	d_id, err := models.InsertRoute(route)
	if err != nil {
		self.ajaxMsg("添加失败", MSG_ERR)
		return
	}
	routeDetail := new(models.AiRouteDetails)
	routeDetail.Dialplan_id = d_id
	routeDetail.Dialplan_detail_tag = self.GetString("tag")
	routeDetail.Dialplan_detail_data = self.GetString("data")
	routeDetail.Dialplan_detail_type_id = 27
	if err != nil {
		self.ajaxMsg("添加明细失败", MSG_ERR)
		return
	}
	_,err = models.InsertRouteDetail(routeDetail)
	self.ajaxMsg("", MSG_OK)
}

func (self *RouteController) AjaxDel(){
	id ,_:= self.GetInt64("id")

	err := models.DelectRoute(id)
	if err != nil {
		self.ajaxMsg("删除失败", MSG_ERR)
		return
	}
	self.ajaxMsg("成功", MSG_OK)
}










